home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / diskmags / 5791-.end / dmg-6260 / articles / sai.doc < prev    next >
Text File  |  1993-07-23  |  10KB  |  216 lines

  1.                    SIMULATED ARTIFICIAL INTELLIGENCE
  2.                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3.  
  4. Have you ever played one of  those  strategy games and wondered how the
  5. programmers managed to make the enemy  decide  what to do next, instead
  6. of just running up and  attacking  you?  Believe  me,  so  have I. So I
  7. decided to create my own  method  of  what I call 'Simulated Artificial
  8. Intelligence' (SAI).
  9.  
  10. My routine contains a list  of  'Resultant  Variables' (RV's), which is
  11. simply a list of  variable  names  -  one  for  each possible action. I
  12. decided to base  my  SAI  on  a  typical  overhead  scene  with several
  13. 'intelligent' characters battling it out with eachother. Eventually, it
  14. works quite well (well...sort of...ish)
  15.  
  16. Here's how it works:
  17.  
  18. Let's say that any given character can perform one of these actions:
  19.  
  20. ATTACK, ADVANCE, RETREAT, CAST FIREBALL, SHOOT ARROW, or PAUSE.
  21.  
  22. For simplicity's sake, we will call the variable names:
  23.  
  24. ATT, ADV, RET, CFR, SHA, PAU.
  25.  
  26. At the beginning of each 'loop', we set each of these RV's to 0%
  27.  
  28. ATT=0 : ADV=0 : RET=0 : CFR=0 : SHA=0 : PAU=0
  29.  
  30. Now, when it  comes  to  the  computer's  turn,  a  collection of other
  31. statistics increase  or  decrease  the  chance  of  each  action  being
  32. performed. Let's introduce these statistics:
  33.  
  34. HEALTH (decreases when hit or attacked, dead if 0)
  35. BRAVERY (measure of cowardice or bravery)
  36. CAST SKILL (how accurate you are at hitting with a fireball)
  37. BOW SKILL (how accurate you are at hitting with an arrow)
  38. WEAPON SKILL (how good a fighter you are)
  39. NUMBER OF FIREBALLS (how many fireballs you have left)
  40. NUMBER OF ARROWS (how many arrows you have left)
  41. WASTEAGE (how much you preserve or waste your fireballs/arrows)
  42.  
  43. Let's take each RV in turn:
  44.  
  45. PROBABILITY OF ATTACKING AN ADJACENT ENEMY (ATT)
  46.  
  47. How healthy you are determines this  up  to  some  extent. If you had a
  48. health value of  80%  or  above,  the  probability  of  attacking would
  49. increase. If health was between 40% and  80%, the attack prob would not
  50. increase by much, and if health  was  below  40%, the attack prob would
  51. decrease.
  52.  
  53. If you are very brave, you will leap straight into battle, thus setting
  54. the attack RV to 90% and over (before modifications of health, etc.) If
  55. you are a coward, the attack prob would drastically lessen.
  56.  
  57. Weapon skill would also make a change.  If you were confident with your
  58. weapon, you would attack with no doubt, whereas if you are weak in this
  59. area, you would be less likely to attack.
  60.  
  61. After these three modifications, you will get a number in ATT. Here are
  62. five examples of character's ATT RV's:
  63.  
  64. Health 100%   Health 80%   Health 60%   Health 40%   Health 20%
  65. Bravery 100%  Bravery 85%  Bravery 75%  Bravery 60%  Bravery 45%
  66. W/skill 100%  W/skill 85%  W/skill 70%  W/skill 55%  W/skill 35%
  67. ~~~~~~~~~~~~  ~~~~~~~~~~~  ~~~~~~~~~~~  ~~~~~~~~~~~  ~~~~~~~~~~~
  68. ATT:100%+     ATT:80%-100% ATT:65%-75%  ATT:40%-65%  ATT:0%-25%
  69.  
  70.  
  71. Next comes PROBABILITY OF ADVANCING TOWARDS CHOSEN TARGET.
  72. Health plays another part here, but  on  a slightly less scale. Bravery
  73. would play a slightly bigger part. The  main thing would be the current
  74. action of the target. If the target was storming towards you, you would
  75. be less likely to advance than if he was running away, screaming.
  76.  
  77. The PROBABILITY OF RETREATING can be  said to be inversely proportional
  78. to the probability of advancing, but  to  make the routine complete, we
  79. calculate the probability separately  like  the  others. Here, we would
  80. include health, bravery, target's current action, etc.
  81.  
  82. The PROBABILITY OF CASTING A FIREBALL  uses different variables. If you
  83. were to find yourself confronting an enemy, one of the first things you
  84. would do is loose off a few fireballs or arrows to soften him up a bit.
  85. We take into consideration how many  fireballs  we have left; if we had
  86. twenty, we would cast loads of them, but  if we only had one or two, we
  87. would save them for later. To  modify  this, we introduce a stat called
  88. 'wasteage'. This determines whether we  waste our long-range resources,
  89. or conserve them. The higher  the  wasteage percentage, the more likely
  90. the  character  is  to  shoot  a  fireball.  Finally,  the  character's
  91. spellcasting ability is taken into consideration.  If  he knows he is a
  92. bad spellcaster, then he is less likely  to  cast one, than if he was a
  93. master. On top of these three,  I  have  also added two more modifiers:
  94. Health state; (if you were weak,  you  would  want your opponent as far
  95. away from you as possible),  and  Target's  current action (ie. keeping
  96. him away if he was advancing,  or  shooting  him  in the back if he was
  97. retreating.)
  98.  
  99. The PROBABILITY OF SHOOTING AN ARROW uses virtually the same methods as
  100. the fireball-casting one  above,  with  these  exceptions.  The average
  101. person carries more arrows than fireballs, and so has more to shoot. It
  102. is easier for the average person to  shoot an arrow rather than to cast
  103. a spell. These all increase the  chance  of  an arrow being shot rather
  104. than a fireball.
  105.  
  106. Finally, THE  PROBABILITY  OF  PAUSING  is  a  'dividing  line' between
  107. advancing and  retreating.  It  is  typically  an  'unsure'  or 'second
  108. thoughts' option, and can be  basically  an  average of the attack prob
  109. and retreat prob.
  110.  
  111. OK, finally we have sorted out the  actions.  We should now have a list
  112. of resultant variables, each containing  a  percentage  value. We add a
  113. random number to each of these  (say  between -10% to 10%) for variety.
  114. Random numbers are also used  often  in  the main equations, but should
  115. not bias the results too greatly. We should also note that the contents
  116. of the RV's should not be taken  as  'gospel'. Even if you were a tough
  117. and brave fighter, you might have a  sudden  urge to shoot an arrow, or
  118. shift position a bit, and so  a 'low-probability alteration' routine is
  119. added near the end. We simply  set  up  some tables which have a direct
  120. link to the current action,  and  say  that  there  is a probability of
  121. maybe  1  in  40  that  the  character  will  suddenly  change  action;
  122. discerning the change in the  environment  (eg. having two more enemies
  123. lumbering up to you, or your current target running off.)
  124.  
  125. Here are some examples of these tables:
  126.  
  127. ACTION:                Attack         Retreat        Shoot arrow
  128.  
  129. LINKED ACTIONS:        Advance        Cast fireball  Cast fireball
  130.                        Cast fireball  Shoot arrow    Advance
  131.                        Shoot arrow    Pause          Retreat
  132.  
  133. *NOT* A LINKED ACTION: Retreat        Advance           ?
  134.  
  135. So, for example, someone who is  retreating may suddenly decide to cast
  136. a fireball, or pause to think 'why am, I running?'
  137.  
  138. At the end of all  of  these  modifications,  we  must now decide which
  139. action is to be taken. The simple way  of doing this is to see which of
  140. the RV's has the biggest number,  or biggest percentage chance of being
  141. carried out. To do  this,  we  take  two  empty variables, MAXVALUE and
  142. VALUE (NB: syntax: max VALUE !)
  143.  
  144. MAXVALUE is set to 0, and so  is  VALUE. MAXVALUE refers to the biggest
  145. probability we have encountered so far,  and VALUE refers to the actual
  146. action which has has the biggest percentage so far.
  147.  
  148. We go through each action, and compare  it's value with MAXVALUE. If it
  149. is lower, then it is discarded,  if  it  is  higher, MAXVALUE is set to
  150. that value, and VALUE is  set  to  the  action  number. This routine is
  151. repeated until all of the actions  have  been  covered.  It is up to you
  152. what equal values determine, (eg. MAXVALUE=80%, NEXT VALUE=80%) whether
  153. they are discarded or used.
  154.  
  155. Now we have determined the current  action  in  the hero's mind, all we
  156. have to do now is write the code  to carry out the action! On this disk
  157. is a program called INTEL.BAS and contains most of the stuff written in
  158. this article.  There  is  a  value  in  line  2  which  holds  how many
  159. characters there are. This can range from 2 to 10.
  160.  
  161. Just to clear up  a  couple  of  loose  ends,  here  is how a character
  162. determines who to advance toward/shoot at, etc.:
  163.  
  164. The method used in my  example  program  is  to  simply  see who is the
  165. closest, and then act upon it.  So,  if  the closest enemy was very big
  166. and strong, you  would  run  off  (ideally),  and  if  weak, attack. To
  167. calculate the closest enemy, run through  each character in turn, using
  168. Pythagoras' Theorem. For those of you  who  chose to sleep through GCSE
  169. maths, here it is:
  170.  
  171. "Pythagoras' Theorem states that the  square of the hypotenuse (longest
  172. side) of a right-angled triangle is  equal  to  the square of the other
  173. two sides."
  174.  
  175. This can be used  to  convert  an  X-  and  Y-axis  difference into one
  176. length. So,
  177.  
  178. HERO POSITION = 2,5
  179. TARGET POSITION = 9,3
  180.  
  181. X DIFFERENCE = max(hpx,tpx)-min(hpx,tpx)   [rids any negative numbers,
  182.                                             you can also use ABS.]
  183. Y DIFFERENCE = max(hpy,tpy)-min(hpy,tpy)   [same applies]
  184.  
  185. XDIFF=7
  186. YDIFF=2
  187.  
  188. DIFFERENCE = SQUARE ROOT OF ( (XDIFF*XDIFF) + (YDIFF*YDIFF) )
  189.  
  190. DIFFERENCE = SQR ( (7*7) + (2*2) )
  191. DIFFERENCE = SQR (49+4)
  192. DIFFERENCE = SQR (53)
  193. DIFFERENCE = 7.280109889
  194.  
  195. DIFFERENCE = 7 SQUARES
  196.  
  197.  
  198. Using the same method of finding  the heighest probability, we find out
  199. the closest  enemy.  Although  the  method  is  the  same,  the initial
  200. MAXVALUE has to be 99, not  0,  because  every target is further than 0
  201. squares away from the character.  We  also  say  if VALUE is lower than
  202. MAXVALUE, and not if VALUE is higher than MAXVALUE. To avoid confusion,
  203. use MINVALUE instead.
  204.  
  205. So, we know who is the target, and we know what action to perform. Once
  206. the action has  been  performed,  the  program  moves  on  to  the next
  207. character and performs the same operation. Once all characters have had
  208. their 'turn', we start all  over  again. This continues until everybody
  209. but one is dead!
  210.  
  211. Check out INTEL.BAS to see  this  whole  thing in motion. and remember,
  212. the routine above is only  for  an  over-head  RPG.  For chess games or
  213. whatever, I'm afraid it's up to you!
  214.  
  215. Article: BLACK EAGLE 23/7/93
  216.